package com.happy3w.ssp.schoolmatehome.controller;

import com.happy3w.ssp.schoolmatehome.component.wx.WxConfig;
import com.happy3w.ssp.schoolmatehome.entity.TagDef;
import com.happy3w.ssp.schoolmatehome.repository.TagDefRepository;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/system")
public class SystemController {

    private final WxConfig wxConfig;
    private final TagDefRepository  tagDefRepository;

    @GetMapping("/info")
    public ResponseEntity<SystemInfo> querySystemInfo() {
        return ResponseEntity.ok(SystemInfo.builder()
                        .appid(wxConfig.getWeb().getAppId())
                        .miniAppId(wxConfig.getMini().getAppId())
                .build());
    }

    @AllArgsConstructor
    @Builder
    @Data
    public static class SystemInfo {
        private String appid;
        private String miniAppId;
    }

    @GetMapping("tags")
    public ResponseEntity<List<String>> queryTags() {
        return ResponseEntity.ok(tagDefRepository.findAllByOrderByOrderIndexAsc()
                .stream()
                .map(TagDef::getTag)
                .collect(Collectors.toList()));
    }

}
